﻿//////////////////////////////////////////////////////////////////
//
// Utilities\ConfigurationHelperProperty.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.ComponentModel;
using System.Configuration;

namespace Utilities
{
   public class ConfigurationHelperProperty<T>
   {
      private ConfigurationHelperElement element_;
      private ConfigurationHelperSection section_;
      private ConfigurationProperty property_;

      public ConfigurationHelperProperty(ConfigurationHelperSection section, string name, T defaultValue)
      {
         section_ = section;
         property_ = section.AddProperty<T>(name, defaultValue);
      }
      public ConfigurationHelperProperty(ConfigurationHelperElement element, string name, T defaultValue)
      {
         element_ = element;
         property_ = element.AddProperty<T>(name, defaultValue);
      }
      public virtual T Value
      {
         get { return (null != section_) ? (T)section_[property_] : (T)element_[property_]; }
         set
         {
            if (null != section_)
            {
               section_[property_] = value;
            }
            else
            {
               element_[property_] = value;
            }
         }
      }
      public static implicit operator T(ConfigurationHelperProperty<T> property)
      {
         return (T)property.Value;
      }
      public override string ToString()
      {
         return Value.ToString();
      }
   }

   ////////////////////////////////////////////////////////////////////////
   /// <summary>
   /// A custom version that encrypts the value stored in the config file
   /// </summary>
   public class ConfigurationHelperEncryptedString : ConfigurationHelperProperty<string>
   {
      public ConfigurationHelperEncryptedString(ConfigurationHelperSection section, string name, string defaultValue)
         : base(section, name, defaultValue)
      {
      }

      public ConfigurationHelperEncryptedString(ConfigurationHelperElement element, string name, string defaultValue)
         : base(element, name, defaultValue)
      {
      }

      public override string Value
      {
         get
         {
            string rawValue = base.Value;
            if (!string.IsNullOrWhiteSpace(rawValue))
            {
               return Encryption.Decrypt(rawValue, true);
            }
            return string.Empty;
         }
         set
         {
            base.Value = string.IsNullOrWhiteSpace(value) ? string.Empty : Encryption.Encrypt(value, true);
         }
      }
   }
}
